import 'dart:convert';
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_with_bmap/entity/result_response.dart';
import 'package:flutter_with_bmap/pages/home/index.dart';
import 'package:flutter_with_bmap/util/global_tool.dart';
import 'package:flutter_with_bmap/util/http/index.dart';
import 'package:flutter_with_bmap/util/storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'entity/login_info.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  // await loginApp();
  //初始化本地存储
  var storage = await SharedPreferences.getInstance();
  Storage.init(storage);
  runApp(const MyApp());
}

Future loginApp() async {
  var result = await HttpUtil.ins6070.post('/api/api/auth/login',
      data: {"username": 'v2xadmin', "password": 'lenovov2x666'},
      options: Options(
          contentType: "application/x-www-form-urlencoded;charset=UTF-8"));
  ResultResponse<LoginInfo> res =
      ResultResponse.fromJson(result, ((json) => LoginInfo.fromJson(json)));
  if (res.code == 0) {
    Storage.setString('token', jsonEncode(res.data?.toJson()));
    GlobalTool.token = res.data;
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        navigatorKey: navigatorKey,
        theme: ThemeData(primarySwatch: Colors.blue),
        home: const HomePage()
        // routes: {
        //   '/A':(context)=>HomePage()
        // },
        );
  }
}
